package problem.year2022.February;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author Liu.Liangyuan 15439
 * @version $Id: Day0213.java, v 0.1 2022年02月13日 10:59 下午 Liangmu Exp $$
 */
public class Day0213 {
    public static void main(String[] args) {

    }

    /**
     * 返回可以拼凑的 balloon 数量
     *
     * @param text 输入自负床
     * @return 返回数量
     */
    public static int maxNumberOfBalloons(String text) {
        String temp = "balloon";
        int len = temp.length();
        if (text.length() < len) return 0;
        char[] chars = temp.toCharArray();
        HashMap<Character, Float> map = new HashMap<>();
        for (char item : chars) {
            map.put(item, 0.0f);
        }
        char[] textArray = text.toCharArray();
        for (char item : textArray) {
            if (map.containsKey(item)) {
                if (item == 'l' || item == 'o') {
                    map.put(item, map.get(item) + 0.5F);
                } else {
                    map.put(item, map.get(item) + 1);
                }
            }
        }
        Float min = map.values().stream().min(Float::compareTo).orElse(0F);
        return (int) Math.floor(min);
    }

    public static int maxNumberOfBalloons2(String text) {
        char[] chars = text.toCharArray();
        int[] res = new int[5];
        for (char item : chars) {
            switch (item) {
                case 'b':
                    res[0]++;
                    break;
                case 'a':
                    res[1]++;
                    break;
                case 'l':
                    res[2]++;
                    break;
                case 'o':
                    res[3]++;
                    break;
                case 'n':
                    res[4]++;
                    break;
            }
        }
        res[2] /= 2;
        res[3] /= 2;
        return Arrays.stream(res).min().getAsInt();
    }
}
